const Error = require('./error')
const storage = require('./storage.js')
const request = require('./request')

const {
    isObject,
    isString,
    isNumber,
    isUndefined,
    isBoolean,
    isArray
} = require('./dataType.js')

const deviceCheckCategory = class deviceCheckCategory {
    constructor() {}

    get(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE_CHECK_CATEGORY.GET.url,
                MS._config.parameters.DEVICE_CHECK_CATEGORY.GET.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    query(options) {
        let params = {
            pageNum: 1,
            pageSize: 10
        }
        if(!isUndefined(options)) {
            if(!isUndefined(options.name) && isString(options.name)) {
                params.name = options.name;
            }
            if(!isUndefined(options.isShip) && isBoolean(options.isShip)) {
                params.isShip = options.isShip;
            }
            if(!isUndefined(options.status) && isBoolean(options.status)) {
                params.status = options.status;
            }
            if(!isUndefined(options.shipCategory) && isNumber(options.shipCategory)) {
                params.shipCategory = options.shipCategory;
            }
            if(!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
                params.pageNum = options.pageNum;
            }
            if(!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
                params.pageSize = options.pageSize;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE_CHECK_CATEGORY.QUERY.url,
                MS._config.parameters.DEVICE_CHECK_CATEGORY.QUERY.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    remove(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE_CHECK_CATEGORY.REMOVE.url,
                MS._config.parameters.DEVICE_CHECK_CATEGORY.REMOVE.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    findEnable(options) {
        let params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.isShip) && isBoolean(options.isShip)) {
                params.isShip = options.isShip;
            }
            if(!isUndefined(options.shipCategory) && isNumber(options.shipCategory)) {
                params.shipCategory = options.shipCategory;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE_CHECK_CATEGORY.FIND_ENABLE.url,
                MS._config.parameters.DEVICE_CHECK_CATEGORY.FIND_ENABLE.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    enable(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE_CHECK_CATEGORY.ENABLE.url,
                MS._config.parameters.DEVICE_CHECK_CATEGORY.ENABLE.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    disable(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE_CHECK_CATEGORY.DISABLE.url,
                MS._config.parameters.DEVICE_CHECK_CATEGORY.DISABLE.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    save(options) {
        let params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.id) && isString(options.id)) {
                params.id = options.id;
            }
            if(!isUndefined(options.name) && isString(options.name)) {
                params.name = options.name;
            }
            if(!isUndefined(options.price) && isNumber(options.price)) {
                params.price = parseInt((options.price * 100));
            }
            if(!isUndefined(options.isShip) && isBoolean(options.isShip)) {
                params.isShip = options.isShip;
            }
            if(!isUndefined(options.status) && isBoolean(options.status)) {
                params.status = options.status;
            }
            if(!isUndefined(options.shipCategory) && isNumber(options.shipCategory)) {
                params.shipCategory = options.shipCategory;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE_CHECK_CATEGORY.SAVE.url,
                MS._config.parameters.DEVICE_CHECK_CATEGORY.SAVE.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }
}

module.exports = deviceCheckCategory